﻿using MoetutuServer.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MoetutuServer.PublishHelper
{
    class CreateContent
    {
        
        /// <summary>
        /// 创建文章，需要tag ，图片，和文章封面。
        /// </summary>
        /// <param name="TagList"></param>
        /// <param name="ImgList"></param>
        /// <param name="ContentCover"></param>
        /// <returns></returns>
        public static string EstablishContent(List<String> TagList, List<string> ImgList)
        {
            String Content = "";
            //文章主体的box
            String Img_box = CreateDiv("img_box", "{0}");
            String DownloadBox = CreateDownloadContent(ImgList);

            try {
                if (ImgList.Count>2 && ImgList[2].IndexOf("upaiyun.com") <0)
                Content = string.Format(Img_box, CreateImgLightBox("img_source pixivbox", ImgList[2], TagList));
                else
                    Content = string.Format(Img_box, CreateImgLightBox("img_source pixivbox",null, TagList));
            }
            catch(Exception ex)
            {

                PrintLog.Log(ex);
            }
            Content += DownloadBox;
        //    Content += CreateTagBox("wp-tag-out", "tag-links",TagList);
            return Content;
        }


        /// <summary>
        /// 创建文章，需要tag ，图片，和文章封面,和版权信息 这里有一个关于数组越界的问题，没添加try catch，注意
        /// </summary>
        /// <param name="TagList"></param>
        /// <param name="ImgList"></param>
        /// <param name="ContentCover"></param>
        /// <returns></returns>
        public static string EstablishContentAndCopyright(List<String> TagList, List<string> ImgList,string Copyright)
        {
       
            String Content = "";
            Content += EstablishContent(TagList,ImgList);
              Content += CreateCopyright(Copyright) ;
     
            return Content;
        }

        private static string CreateCopyright(string copyright)
        {
         
            string Format = CreateDiv("copyright",string.Format("<p><a href=\"{0}\">转载来源</a></p>", copyright));   
          
            return Format;
        }

        /// <summary>
        /// 创建用户搜索的盒子
        /// </summary>
        /// <param name="ULName"></param>
        /// <param name="LIName"></param>
        /// <param name="TagList"></param>
        /// <returns></returns>
        private static string CreateTagBox(String ULClass, String LIClass, List<String> TagList) {
            String ULHtml = string.Format("<ul class='{0}'>{1}</ul>", ULClass, "{0}");
            string LiHtml = string.Format("<li class='{0}'>{1}</li>", LIClass,"{0}");
            //https://pic.moetutu.com/?s=%E5%9B%BE%E7%89%87
            String ContentHtml = "";
            foreach (String line in TagList) {
                String Link= "https://pic.moetutu.com/?s=" + System.Web.HttpUtility.UrlEncode(line,Encoding.UTF8);
                ContentHtml += string.Format(LiHtml, CreateAnchor("tag-list", Link, line));
             
            }

            ContentHtml = string.Format(ULHtml, ContentHtml);
            return ContentHtml;

        }

        /// <summary>
        /// 生成img标签，并且用lightBox进行包裹
        /// </summary>
        /// <param name="ClassName"></param>
        /// <param name="ImgSrc"></param>
        /// <param name="TagList"></param>
        /// <returns></returns>
        private static string CreateImgLightBox(String ClassName, String ImgSrc, List<string> TagList)
        {
            /*
             <a target='_blank' href='{0}' data-lightbox='img' data-title='{1}'>{2}</a>
             <img class="img_source" src="https://pic.moetutu.com/wp-content/uploads/2018/02/Yashajin.Ai_.full_.2259024_Thumbnail_600.jpg" alt="Pixiv Id 10450946,Ryuuou no Oshigoto!,Yashajin Ai,"></a>*/
            String TagText = ContentMerge(TagList, ",");
            String Imghtml = String.Format("<img class='{0}' src='{1}' alt='{2}'>", ClassName, StaticValue.ReplaceFlag, TagText);

            string AnchorHtml =  "";
            if(ImgSrc!=null)
                AnchorHtml = string.Format("<a target='_blank' href='{0}' data-lightbox='img' data-title='{1}'>{2}</a>",ImgSrc, TagText, "{0}");
            else
                AnchorHtml = string.Format("<a target='_blank' href='{0}' data-lightbox='img' data-title='{1}'>{2}</a>", StaticValue.ReplaceFlag, TagText, "{0}");

            return string.Format(AnchorHtml,Imghtml);

        }

        

        /// <summary>
        /// 生成img标签的方法
        /// </summary>
        /// <param name="ClassName"></param>
        /// <param name="ImgSrc"></param>
        /// <param name="TagList"></param>
        /// <returns></returns>
        private static string CreateImg(String ClassName, String ImgSrc, List<string> TagList) {
            String Imghtml = String.Format("<img class='{0}' src='{1} alt='{2}'>", ClassName, ImgSrc, ContentMerge(TagList, ","));
            return Imghtml;

        }
        /// <summary>
        /// 创建下载的盒子，和赋值相关链接
        /// </summary>
        /// <param name="imgList"></param>
        /// <returns></returns>
        private static string CreateDownloadContent(List<string> imgList)
        {
            //放置下载链接的box
            String Original_img = CreateDiv("Original_img", "{0}");
            //来源的链接

   
            String NewContent = "";
            for (int i = 0; i < imgList.Count; i++)
            {
                String ButtonText = "";
                if (imgList[i].IndexOf("sinaimg") > 0)
                    ButtonText = "国内节点" + i;
                else
                    ButtonText = "国外节点" + i;

                if (imgList[i] == "")
                    continue;
                if (i == 0)
                    NewContent+= CreateAnchor("Original_img_url", imgList[i], "转载来源");
                else if (i == 1)
                    NewContent += CreateAnchor("sina_compress", imgList[i], ButtonText);
              
                else 
                     NewContent += CreateAnchor("Original_sina_compress", imgList[i], ButtonText);
            
               
            }
            //合并
            Original_img = string.Format(Original_img, NewContent);


            return Original_img;
        }
        /// <summary>
        /// 合并list 到sting 默认分隔符为一个空格
        /// </summary>
        /// <param name="ContentList"></param>
        /// <param name="Delimiter"></param>
        /// <returns></returns>
        private static String ContentMerge(List<string> ContentList, String Delimiter = " ")
        {
            String Content = HtmlHelper.GetFirstValue(ContentList);
            return Content;
        }

        /// <summary>
        /// 创建A标签，默认跳出到新窗口
        /// </summary>
        /// <param name="ClassName"></param>
        /// <param name="Href"></param>
        /// <param name="ContentText"></param>
        /// <returns></returns>
        private static string CreateAnchor(String ClassName, String Href, String ContentText)
        {

            String AcnhorHtml = String.Format("<a class='{0}'  rel=\"noreferrer\" target='_blank' href='{1}'>{2}</a>", ClassName, Href, ContentText);
            return AcnhorHtml;
        }
        /// <summary>
        /// 创建div
        /// </summary>
        /// <param name="ClassName"></param>
        /// <returns></returns>
        private static string CreateDiv(String ClassName, string Content)
        {

            string DivHtml = string.Format("<div class='{0}'>{1}</div>", ClassName, Content);
            return DivHtml;
        }
    }


}
